// For compilers that support precompilation, includes "wx/wx.h".
#include "wx/wxprec.h"

#ifdef __BORLANDC__
    #pragma hdrstop
#endif

// for all others, include the necessary headers (this file is usually all you
// need because it includes almost all "standard" wxWidgets headers)
#ifndef WX_PRECOMP
    #include "wx/wx.h"
#endif
#include "DLinkable.h"



DLinkable::DLinkable(wxString name, wxRect rct)
{
	m_name = name;
	m_rect = rct;
	m_number = -1;
	/*m_texta = new DText();
	m_texta->SetName(name.c_str());
	m_texta->SetPosition(&wxPoint(rct.x + 5, rct.y + 5));*/
	this->m_brush = wxBrush(wxColor(255, 200, 200));
}


DLinkable::~DLinkable(void)
{
	/*if(m_texta != NULL)
	{
		delete m_texta;
		m_texta = NULL;
	}*/
}

void DLinkable::Draw(wxDC *dc)
{
	if(this->IsSelected() == TRUE)
	{
		this->SetDrawingTools(dc);
		dc->SetPen(wxPen(wxColor(255, 0, 0), 1, wxDOT));
		dc->DrawRectangle(m_rect);
		dc->DrawText(m_name, m_rect.x + 5, m_rect.y + 5);
		dc->DrawText(wxString::Format(wxT("%d"), m_number), m_rect.x + m_rect.width / 2, m_rect.y + m_rect.height / 2);
	}
	else
	{
		this->SetDrawingTools(dc);
		dc->DrawRectangle(m_rect);
		dc->DrawText(m_name, m_rect.x + 5, m_rect.y + 5);
		dc->DrawText(wxString::Format(wxT("%d"), m_number), m_rect.x + m_rect.width / 2, m_rect.y + m_rect.height / 2);
	}
}

bool DLinkable::PointInside(wxPoint *pnt)
{
	if(m_rect.Inside(*pnt))
		return true;
	else
		return false;
}

float DLinkable::GetArea()
{
	return (float)(m_rect.width * m_rect.height);
}

void DLinkable::ConvertToText(wxString *str)
{
	*str << wxT("linkable\t") << this->m_rect.x << wxT("\t") << m_rect.y << wxT("\t") << this->m_rect.width << wxT("\t") << m_rect.height << wxT("\t") << m_name.c_str() << wxT("\n");
}

wxPoint DLinkable::GetTopLeft()
{
	return wxPoint(this->m_rect.x, this->m_rect.y);
}

void DLinkable::RemoveConnection(int id)
{
	for(INT_ITER i = m_idCont.begin(), s = m_idCont.end(); i<s; i++ )
	{
		if((*i) == id)
		{
			m_idCont.erase(i);
			break;
		}
	}
}

bool DLinkable::IsConnectedWith(int id)
{	
	bool connect = false;
	for(INT_ITER i = m_idCont.begin(), s = m_idCont.end(); i<s; i++ )
	{
		if((*i) == id)
		{
			connect = true;
			break;
		}
	}

	return connect;
}

wxString DLinkable::GetName()
{
	return m_name;
}

wxPoint DLinkable::GetBottomRight()
{
	wxPoint pnt;
	pnt.x = m_rect.x + m_rect.width;
	pnt.y = m_rect.y + m_rect.height;
	return pnt;
}

void DLinkable::SetNumber(int num)
{
	this->m_number = num;
}

int DLinkable::GetNumber()
{
	return m_number;
}